package com.duanjw.java.algorithm;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * @author duanjw
 */
public class TreeNodeMap extends HashMap<String,List<TreeNodeMap>> {
    public static String [] alias;
    public int id;
    private String name;
    private List<TreeNodeMap> nodeList;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<TreeNodeMap> getNodeList() {
        return nodeList;
    }

    public void setNodeList(List<TreeNodeMap> nodeList) {
        this.nodeList = nodeList;
    }

    public TreeNodeMap(int id, String name, List<TreeNodeMap> nodeList) {
        this.id = id;
        this.name = name;
        this.nodeList = nodeList;
    }

    @Override
    public String toString() {
        return "TreeNodeMap{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", nodeList=" + nodeList +
                '}';
    }

    private String str(){
        return
                "id=" + id +
                        ", name='" + name + '\'';
    }

    public static String toString(List<TreeNodeMap> treeNodes) {
        String result = "";
        for (TreeNodeMap treeNode : treeNodes) {
            result += find(treeNode, "");
        }
        return result;
    }

    public static String find(TreeNodeMap treeNode, String s) {
        s += treeNode.str();
        if(treeNode.nodeList==null) {
            for (String aril : alias) {
                treeNode.nodeList = treeNode.get(aril);
                if (treeNode.nodeList != null) {
                    break;
                }
            }
        }
        if(treeNode.nodeList==null) {
            return s;
        }
        if(treeNode.nodeList!=null) {
            for (TreeNodeMap node : treeNode.nodeList) {
                s = find(node, s);
            }
        }
        return s;
    }

    public static void main(String[] args) {
        TreeNodeMap.alias = new String[]{"aaa","bbb"};
        TreeNodeMap two = new TreeNodeMap(21, "张三", null);
        TreeNodeMap four = new TreeNodeMap(23, "孙七", null);

        four.put("bbb", Arrays.asList(two));
        List<TreeNodeMap> root = new ArrayList<>();
        root.add(new TreeNodeMap(3, "王八", Arrays.asList(four)));

        System.out.println("分解前："+root.toString());

        System.out.println("分解后"+ TreeNodeMap.toString(root));

        System.out.println("转换后："+root.toString());

    }
}
